package com.cafe.case9;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;

@Slf4j
public class RetryDemo {

    public static boolean sendPayMessage(PayMessage payMessage) {
        // 模拟发送失败的情况
        if (payMessage.getRetryCount() < 6) {
            log.info("支付回调失败：{}", JSONUtil.toJsonStr(payMessage));
            return false;
        }
        log.info("支付回调成功：{}", JSONUtil.toJsonStr(payMessage));
        return true;
    }

    public static void main(String[] args) {
        int retryCount = 0;
        PayMessage payMessage = new PayMessage();
        payMessage.setOrderNo("12345678");
        payMessage.setPayStatus("SUCCESS");
        payMessage.setPayTime(LocalDateTime.now());
        payMessage.setRetryCount(retryCount);

        while (retryCount < 10) {
            boolean isOk = sendPayMessage(payMessage);
            if (isOk) {
                break;
            }
            try {
                log.info("第{}次尝试回调，等待{}ms后重试", retryCount, RetryConfig.getRetryInterval(retryCount));
                Thread.sleep(RetryConfig.getRetryInterval(retryCount));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            payMessage.setRetryCount(++retryCount);
        }
    }
}
